package com.liuwei.msmall.marketing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liuwei.msmall.common.dto.SkuFullReductionDTO;
import com.liuwei.msmall.common.utils.PageUtils;
import com.liuwei.msmall.common.utils.Query;
import com.liuwei.msmall.marketing.dao.SkuFullReductionDao;
import com.liuwei.msmall.marketing.entity.MemberPriceEntity;
import com.liuwei.msmall.marketing.entity.SkuFullReductionEntity;
import com.liuwei.msmall.marketing.service.MemberPriceService;
import com.liuwei.msmall.marketing.service.SkuFullReductionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Resource
    MemberPriceService memberPriceService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveCouponInfo(List<SkuFullReductionDTO> dtos) {

        if (CollectionUtils.isEmpty(dtos)){
            return;
        }

        List<MemberPriceEntity> memberPriceEntities = new ArrayList<>();

        List<SkuFullReductionEntity> skuFullReductionEntities = new ArrayList<>(dtos.size());

        dtos.stream().forEach(item->{

            List<SkuFullReductionDTO.MemberPriceVo> memberPrice = item.getMemberPrice();
            /**
             * 封装商品对应会员价格信息
             */
            List<MemberPriceEntity> collect = memberPrice.stream().filter(i -> i.getPrice().compareTo(new BigDecimal("0")) == 1).map(memberPriceVo -> {
                MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
                memberPriceEntity.setMemberLevelId(memberPriceVo.getId());
                memberPriceEntity.setMemberPrice(memberPriceVo.getPrice());
                memberPriceEntity.setSkuId(item.getSkuId());
                return memberPriceEntity;
            }).collect(Collectors.toList());
            memberPriceEntities.addAll(collect);
            /**
             * 满减优惠信息 ,如果满减价大于0则保存
             */
//            if (item.getFullPrice().compareTo(new BigDecimal("0")) == 1 && item.getReducePrice().compareTo(new BigDecimal("0")) == 1){
//                SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
//                skuFullReductionEntity.setSkuId(item.getSkuId());
//                skuFullReductionEntity.setFullPrice(item.getFullPrice());
//                skuFullReductionEntity.setReducePrice(item.getReducePrice());
//                skuFullReductionEntities.add(skuFullReductionEntity);
//            }

        });

        /**
         * 批量保存优惠信息
         */
        if (!CollectionUtils.isEmpty(skuFullReductionEntities)) {
            this.saveBatch(skuFullReductionEntities);
        }

        if (!CollectionUtils.isEmpty(memberPriceEntities)) {
            memberPriceService.saveBatch(memberPriceEntities);
        }


    }

}